<?php
namespace pay\wxpay;
require(__DIR__.'/lib/WxPayData.php');
use pay\wxpay\WxpayConfig;
use pay\wxpay\lib\WxPayApi;
use pay\wxpay\lib\WxPayUnifiedOrder;
use pay\wxpay\lib\WxPayOrderQuery;
use pay\wxpay\example\JsApiPay;

/**
 * 微信支付
 * @package wxpay
 */
class Wxpay
{
	// 支付
	public function index($body)
	{
		//$config = new WxpayConfig();
		$pay = new WxPayApi();
		$openId = $body['openid'];
	
		$JsApiPay = new JsApiPay();

		//②、统一下单
		$input = new WxPayUnifiedOrder();
		$input->SetBody($body['body']);
		$input->SetAttach($body['attach']);
		$input->SetOut_trade_no($body['trade_no']);
		$input->SetTotal_fee($body['money']);
		$input->SetTime_start(date("YmdHis"));
		$input->SetTime_expire(date("YmdHis", time() + 600));
		$input->SetGoods_tag($body['tag']);
		$input->SetNotify_url($body['notify_url']);
		$input->SetTrade_type("JSAPI");
		$input->SetOpenid($openId);
	
		$config = new WxPayConfig();
		
		$order = $pay->unifiedOrder($config,$input);

		// 重新创建
		$jsApiParameters = $JsApiPay->GetJsApiParameters($order);

		return $jsApiParameters;
	}
	// 查询订单
	public function orderquery($body,$type=2)
	{
		$config = new WxpayConfig();
		$pay = new WxPayApi();
		$JsApiPay = new JsApiPay();
		// 查询订单
		$input = new WxPayOrderQuery();
		
		if($type==1){
			// 用平台订单查询
			$input->SetTransaction_id($body['transaction_id']);
		}else{
			// 用商家订单查询
			$input->SetOut_trade_no($body['out_trade_no']);
		}
		$result = $pay->orderQuery($config, $input);
		return $result;
	}

}  
